package com.gitee.faster.webdav.kernel.protocol.dispatcher;

import com.gitee.faster.webdav.common.constant.Constants;
import com.gitee.faster.webdav.common.constant.HttpHeaderEnum;
import com.gitee.faster.webdav.common.constant.MethodEnum;
import com.gitee.faster.webdav.kernel.protocol.HttpExchange;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 获取所有允许的方法名称
 *
 * @author hongda.li
 */
public class MethodDispatcherOptions implements MethodDispatcher {

    /**
     * 所有允许的方法名称，用逗号分隔
     */
    private final String allowedMethodName = Arrays.stream(MethodEnum.values())
            .map(Enum::name)
            .collect(Collectors.joining(Constants.SPLIT_COMMA_SPACE));

    @Override
    public MethodEnum supported() {
        return MethodEnum.OPTIONS;
    }

    @Override
    public void dispatch(HttpExchange accessor) {
        accessor.addHeader(HttpHeaderEnum.ALLOW.getHeader(), this.allowedMethodName);
    }
}
